package com.zpb.admin.controller;

import com.zpb.framework.domain.dto.UserAddDto;
import com.zpb.framework.domain.dto.UserQueryDto;
import com.zpb.framework.domain.ApiResult;
import com.zpb.framework.domain.dto.UserUpdateDto;
import com.zpb.framework.domain.enums.ResultCodeEnum;
import com.zpb.framework.domain.vo.PageVo;
import com.zpb.framework.domain.vo.UserWithRoleVo;
import com.zpb.framework.service.UserService;
import com.zpb.framework.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Description:
 * @Author: zhaopengbo
 * @Date: 2025/9/22
 */
@RestController
@RequestMapping("/system/user")
public class SysUserController {
    @Autowired
    private UserService userService;

    @GetMapping("list")
    public ApiResult<PageVo> getUserList(@RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize,
                                         UserQueryDto userQueryDto){
        return userService.getUserList(pageNum,pageSize,userQueryDto);
    }
    @PostMapping
    public ApiResult addUser(@RequestBody UserAddDto userAddDto){
        if (userAddDto == null) {
            return ApiResult.failure(ResultCodeEnum.MISS_PARAM);
        }
        if (!StringUtils.hasText(userAddDto.getUsername())){
            return ApiResult.failure(ResultCodeEnum.NO_USERNAME);
        }
        if (!StringUtils.hasText(userAddDto.getPassword())){
            return ApiResult.failure(ResultCodeEnum.NO_PASSWORD);
        }
        if (!StringUtils.hasText(userAddDto.getEmail())){
            return ApiResult.failure(ResultCodeEnum.NO_EMAIL);
        }
        if (!StringUtils.hasText(userAddDto.getNickName())){
            return ApiResult.failure(ResultCodeEnum.NO_NICKNAME);
        }
        return userService.addUser(userAddDto);
    }
    @DeleteMapping("/{id}")
    public ApiResult deleteUser(@PathVariable Long id){
        if(Objects.equals(id, SecurityUtils.getCurrentId())){
            return ApiResult.failure(ResultCodeEnum.CAN_NOT_DEL_SELF);
        }
        return userService.delUser(id);
    }

    @GetMapping("/{id}")
    public ApiResult<UserWithRoleVo> getUser(@PathVariable Long id){
        return userService.getUserWithRoleById(id);
    }
    @PutMapping
    public ApiResult updateUser(@RequestBody UserUpdateDto userUpdateDto){
        return userService.updateUserById(userUpdateDto);
    }
}
